const fs = require('fs')
const path = require('path')
const http = require('http')
const { rejects } = require('assert')

let pathurl = path.join(__dirname, '../files/chengji-ok.txt')

let app = http.createServer((req,res)=> {
    res.setHeader("Content-Type", 'text/html;charset=utf-8') // 设置编码格式 解决中文乱码的问题

    /* new Promise((resolve,reject)=> {
        fs.readFile(pathurl,'utf8',(err,data)=> {
            if(err) {
                reject(err)
            } else {
                resolve(data)
            }
        })
    }).then(data=> {
        res.write(data)
        res.end()
    }).catch((err)=> console.log(err)) */

  /*   new Promise((resolve,reject)=> {
        fs.readFile(pathurl,'utf8',(err,data)=> {
            if(err) {
                reject(err)
            } else {
                resolve(data)
            }

        })
    }).then(data=> {
        res.write(data)
        res.end()
    }).catch(err=> console.log(err)) */

    new Promise((resolve,reject)=> {
        fs.readFile(pathurl,'utf8',(err,data)=> {
            if(err) {
                reject(err)
            } else {
                resolve(data)
            }

        })
    }).then(data=> {
        res.write(data)
        res.end()
    }).catch(err=> console.log(err))

})

app.listen(3000, () => {
    console.log('3000 run');
})